"""
Case Type   : 基础功能
Case Name   : 修改行存表索引压缩类型和等级,修改压缩类型报错
Create At   : 2022/07/25
Owner       : liu-tong-8848
Description :
    1.创建行存表
    2.创建索引
    3.修改压缩类型
    4.修改Compress_level
    5.清理环境
Expect      :
    1.成功
    2.成功
    3.合理报错
    4.成功
    5.成功
History     :
"""
import os
import unittest
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class DdlTestCase(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.com = Common()
        self.constant = Constant()
        self.comshpri = CommonSH('PrimaryDbUser')
        self.tb_name1 = "t_index_bypass_0258_01"
        self.tb_name2 = "t_index_bypass_0258_02"
        self.index = "i_bypass_0258"

    def test_index_bypass(self):
        self.log.info(f'----{os.path.basename(__file__)} start----')
        text = '----step1: 创建行存表 expect:成功----'
        self.log.info(text)
        create_table = self.comshpri.execut_db_sql(
            f'''drop table if exists {self.tb_name1};
            drop table if exists {self.tb_name2};
            create table {self.tb_name1}( c1 int,c2 varchar2,c3 number,
            c4 money,c5 char(20),c6 clob,c7 blob,c8 date,c9 boolean,
            c10 timestamp,c11 point,columns12 cidr) with (compresstype=2, 
            compress_chunk_size=512)
            partition by list(c1) subpartition by range(c3)
            (
                partition ts1 values(1,2,3,4,5)
                (
                    subpartition ts11 values less than(500),
                    subpartition ts12 values less than(5000),
                    subpartition ts13 values less than(maxvalue)
                ),
                partition ts2 values(6,7,8,9,10),
                partition ts3 values(11,12,13,14,15)
                (
                    subpartition ts31 values less than(5000),
                    subpartition ts32 values less than(10000),
                    subpartition ts33 values less than(maxvalue)
                ),
                partition ts4 values(default)
            );''')
        self.log.info(create_table)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, create_table,
                      '执行失败' + text)

        text = '----step2: 创建索引 expect:成功----'
        self.log.info(text)
        create_index = self.comshpri.execut_db_sql(
            f'''drop index if exists {self.index};
            create unique index {self.index} on {self.tb_name1}(c1 nulls 
            first,c2,c3) global with(fillfactor=80,compresstype=2,
            compress_chunk_size=512,compress_byte_convert=1,
            compress_diff_convert=1);''')
        self.log.info(create_index)
        self.assertIn(self.constant.DROP_INDEX_SUCCESS_MSG, create_index,
                      '执行失败' + text)
        self.assertIn(self.constant.CREATE_INDEX_SUCCESS_MSG, create_index,
                      '执行失败' + text)

        text = '----step3: 修改压缩类型 expect:合理报错----'
        self.log.info(text)
        alter_data = self.comshpri.execut_db_sql(
            f'''alter index {self.index} set(compresstype=1);''')
        self.log.info(alter_data)
        self.assertIn('ERROR:  change compresstype OPTION is not supported',
                      alter_data, '执行失败' + text)

        text = '----step4: 修改Compress_level expect:成功----'
        self.log.info(text)
        alter_data = self.comshpri.execut_db_sql(
            f'''alter index {self.index} set(Compress_level=3);''')
        self.log.info(alter_data)
        self.assertIn(self.constant.ALTER_INDEX_SUCCESS_MSG, alter_data,
                      '执行失败' + text)

    def tearDown(self):
        text = "----step5:环境清理 expect:成功----"
        self.log.info(text)
        clean_eviro = self.comshpri.execut_db_sql(
            f'''drop table {self.tb_name1} cascade;''')
        self.log.info(clean_eviro)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, clean_eviro,
                      '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
